package com.anolesoft.fourteenWaiXie.mapper;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.fourteenWaiXie.dto.QuantityLogDTO;
import com.anolesoft.fourteenWaiXie.dto.QuantityManagementDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface QuantityServiceMapper {
    //根据制造记录条码查询表单信息
    @Select("select a.fabrecordid, a.assignmentid, a.jx, a.partid, a.pc, a.jc, a.pcjc, a.putnum, a.isfromtemp, " +
            "a.partstate, a.shibang, a.shiliao_num, a.trial_num, a.ddj_num, a.pohuai_num, a.jieyu_num, a.pgh2, " +
            "'投产'|| a.putnum || '；试棒' || a.shibang || '；试料' || a.shiliao_num || '；试件' || a.trial_num || " +
            "'；对刀' || a.ddj_num || '；结余' || a.jieyu_num as total, " +
            "a.putnum + a.shibang + a.shiliao_num + a.trial_num + a.ddj_num - a.pohuai_num + a.jieyu_num as totalnum " +
            "from mes_order_trace a " +
            "where a.fabrecordid = #{fabrecordid, jdbcType=VARCHAR} " +
            "and a.delete_flag = 0")
    QuantityManagementDTO queryQuantity(@Param("fabrecordid") String fabrecordid);
    // 更新制造记录表投产数量信息
    @Update("update mes_order_trace a set a.putnum = #{quantityManagementDTO.putNum, jdbcType=INTEGER}, " +
            "a.shibang = #{quantityManagementDTO.shibang, jdbcType=INTEGER}, " +
            "a.shiliao_num = #{quantityManagementDTO.shiliaoNum, jdbcType=INTEGER}, " +
            "a.trial_num = #{quantityManagementDTO.trialNum, jdbcType=INTEGER}, " +
            "a.ddj_num = #{quantityManagementDTO.ddjNum, jdbcType=INTEGER}, " +
            "a.jieyu_num = #{quantityManagementDTO.jieyuNum, jdbcType=INTEGER}, " +
            "a.assignmentid = #{quantityManagementDTO.newAssignmentId, jdbcType=VARCHAR}, " +
            "a.pgh2 = #{quantityManagementDTO.pgh2, jdbcType=VARCHAR} " +
            "where a.fabrecordid = #{quantityManagementDTO.fabrecordid, jdbcType=VARCHAR}")
    int updateQuantity(@Param("quantityManagementDTO") QuantityManagementDTO quantityManagementDTO);
    //新增修改日志信息
    @Insert("insert into mes_wxslgg_log b (b.fabrecordid, b.assignmentid, b.jx, b.partid," +
            " b.pcjc, b.total, b.newtotal, b.operationuid, b.operationtime, b.factory_id, b.newassignmentid)" +
            "values (#{quantityLogDTO.fabrecordid, jdbcType=VARCHAR}, " +
            "#{quantityLogDTO.assignmentId, jdbcType=VARCHAR}, " +
            "#{quantityLogDTO.jx, jdbcType=VARCHAR}, " +
            "#{quantityLogDTO.partId, jdbcType=VARCHAR}, " +
            "#{quantityLogDTO.pcjc, jdbcType=VARCHAR}, " +
            "#{quantityLogDTO.total, jdbcType=VARCHAR}, " +
            "#{quantityLogDTO.newTotal, jdbcType=VARCHAR}, " +
            "#{quantityLogDTO.operationUid, jdbcType=VARCHAR}, " +
            "sysdate, " +
            "#{quantityLogDTO.factory_id, jdbcType=VARCHAR}, " +
            "#{quantityLogDTO.newAssignmentId, jdbcType=VARCHAR} )")
    int insertQuantityLog(@Param("quantityLogDTO") QuantityLogDTO quantityLogDTO);
    //查询修改日志表
    @SelectProvider(type = createSQL.class, method = "queryQuantityLogSQL")
    List<QuantityLogDTO> queryQuantityLog(@Param("quantityLog") QuantityLogDTO quantityLog, Page<QuantityLogDTO> page);
    public static class createSQL{
        public static String queryQuantityLogSQL(@Param("quantityLog") QuantityLogDTO quantityLog){
            return new SQL(){
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append(" b.*,c.name from mes_wxslgg_log b inner join stf_employee c ");
                    sql.append(" on c.objuid = b.operationuid where 1 = 1 ");
                    if (!StrUtil.isEmptyOrUndefined(quantityLog.getFabrecordid())){
                        sql.append(" and b.fabrecordid = #{quantityLog.fabrecordid, jdbcType=VARCHAR} ");
                    }
                    if (!StrUtil.isEmptyOrUndefined(quantityLog.getJx())){
                        sql.append(" and b.jx = upper(#{quantityLog.jx, jdbcType=VARCHAR}) ");
                    }
                    if (!StrUtil.isEmptyOrUndefined(quantityLog.getPartId())){
                        sql.append(" and b.partid = upper(#{quantityLog.partId, jdbcType=VARCHAR}) ");
                    }
                    sql.append(" order by b.operationtime desc ");
                    SELECT(sql.toString());
                    System.out.println(">>queryQuantityLogSQL:" + sql.toString());
                }
            }.toString();
        }
    }
}
